#pragma once
#include <stdio.h>
#include <string>

using namespace std;

class MaterialLList
{
	typedef struct Material
	{
		int id;
		string mName;
		string tName;
		Material* Next;
		Material() { Next=NULL; mName=""; tName=""; id=0; }
	};

	typedef Material* MaterialListPtr;
	
public:
	Material* Head;
	Material* Tail;
	Material* CurrentPtr;

	MaterialLList()
	{
		Head = new Material;
		Tail = Head;
		CurrentPtr = Head;
	};

	void AddANode()
	{
		Tail->Next = new Material;
		Tail->Next->id = Tail->id + 1;
		Tail = Tail->Next;
		CurrentPtr = Tail;
	};

	MaterialListPtr Previous(MaterialListPtr index)
	{
		MaterialListPtr temp=Head;
		if(index==Head) //special case, index IS the head :)
		{
			return Head;
		}

		while(temp->Next != index)
		{
			temp=temp->Next;
		}

		return temp;
	};

	void Advance()
	{
		CurrentPtr=CurrentPtr->Next;
	};

	void Rewind()
	{
		if(CurrentPtr != Head)
		{
			CurrentPtr=Previous(CurrentPtr);
		}
	};

	/*void GetId(int _id)
	{
		CurrentPtr = Head;
		do
		{
			Advance();
		}while(CurrentPtr->id != _id && CurrentPtr != Tail);
	}*/

	Material* GetId(int _id)
	{
		CurrentPtr = Head;
		do
		{
			Advance();
		}while(CurrentPtr->id != _id && CurrentPtr != Tail->Next);

		return CurrentPtr;
	}

	void DeleteANode(MaterialListPtr corpse)
	{
		MaterialListPtr temp;

		if(corpse == Head) //case 1 corpse = Head
		{
			temp=Head;
			Head=Head->Next;
			delete temp;
		}
		else if(corpse == Tail) //case 2 corpse is at the end
		{
			temp = Tail;
			Tail=Previous(Tail);
			Tail->Next=NULL;
			delete temp;
		}
		else //case 3 corpse is in middle somewhere
		{
			temp=Previous(corpse);
			temp->Next=corpse->Next;
			delete corpse;
		}

		CurrentPtr=Head; //Reset the class tempptr
	};

	~MaterialLList()
	{
		MaterialListPtr temp = Head;
		CurrentPtr = Head;

		while(CurrentPtr != NULL)
		{
			CurrentPtr = CurrentPtr->Next;
			delete temp;
			temp=CurrentPtr;
		}
	};

};